Repository navigation
Map constructor keeps a repeated key's last value; a looping chain throws - #101
Merged
Merged
Conversation
…rows
new Map([["a", 1], ["a", 3]]).get("a") was 1: the constructor inserted with
ThrowOnExisting, whose branch was an empty stub that skipped the entry. It
inserts with OverwriteExisting now, so the last value wins and the key keeps
its first place, as in JavaScript.
ThrowOnExisting is gone: nothing else needed it. Set's constructor and add
passed it too, and for them an equal value was already left alone; they pass
None.
The six places that find a chain of entries going round (a Map or a Set
changed by another thread while this one reads it) were comments saying
"throw exception". Two of them, in the insert loops, did nothing at all, so
the loop would not have ended. They throw an Error now.
map_constructor_duplicates.ts covers the constructors.
Tested in release on Windows x64 for gc, rc, none and own, compiled and under
the JIT; debug, Linux, x86 and wasm were not built locally.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The remaining known loose ends in
MapandSet, in one PR.What
new Mapwith a repeated key keeps the last value.new Map([["a", 1], ["b", 2], ["a", 3]]).get("a")was1; it is3now, and the key keeps its first place, as in JavaScript.Error. Six places detect it and only had a comment saying "throw exception".Why
Mapconstructor inserted withInsertionBehavior.ThrowOnExisting. That branch was an empty stub, so a repeated key was silently skipped. It inserts withOverwriteExistingnow.Map.tryInsert,Set.AddIfNotPresent) the check had an empty body, so the loop would never have ended. The lookups and removals returned "not found".Change
src/generics/lib.generics.ts:InsertionBehavior.ThrowOnExistingis removed; nothing else needed it.Set's constructor andaddpassed it and now passNone. Their behaviour is unchanged: an equal value was already left alone.new Error("Map or Set changed by another thread while in use").tests/map_constructor_duplicates.ts(new): repeated keys in theMapconstructor, with string and number keys, and repeated values in theSetconstructor.For the reviewer
InsertionBehavioris not declared inlib.d.ts, so no declaration changes.Testing
%of two integers of any width is an integer remainder TypeScriptCompiler#852; this change does not depend on it.🤖 Generated with Claude Code